﻿using CustomPlayerEffects;
using PlayerRoles;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using RoundEvent.Main;
using System;
using System.ComponentModel;
using UnityEngine;
using Utils;


namespace RoundEvent.Event
{
    public class RedEvent
    {
        string[] commands = new string[] { "event", "eve" };

        uint killCount = 0;


        [PluginEvent(ServerEventType.RemoteAdminCommandExecuted)]
        public void OnRemoteCommandRun(RemoteAdminCommandExecutedEvent ev)
        {
            string command = ev.Command.ToLower();

            if (!commands.Contains(command)) return;

            if (ev.Arguments[0] != "red") return;


            if (ev.Arguments.Length == 1)
            {
                this.DoNotice();
                return;
            }


            if (ev.Arguments.Length == 2 && ev.Arguments[1] == "done")
            {
                this.DoNoticeDone();
                TimeCoroutine.Stop(this.m_COR);
                return;
            }
        }

        [PluginEvent(ServerEventType.RoundStart)]
        public void OnRoundStart(RoundStartEvent ev)
        {
            TimeCoroutine.Stop(this.m_COR);
            killCount = 0;
        }

        [PluginEvent(ServerEventType.RoundEnd)]
        public void OnRoundEnd(RoundEndEvent ev)
        {
            TimeCoroutine.Stop(this.m_COR);

            killCount = 0;
        }




        [PluginEvent(ServerEventType.PlayerDeath)]
        public void OnPlayerDeath(PlayerDeathEvent ev)
        {
            if (Round.IsLocked) return;

            if (ev.Attacker == null) return;
            if (ev.Attacker.Team != PlayerRoles.Team.SCPs) return;

            killCount++;

            IsHappened();
        }


        [Description("统计数目")] uint count = 0;
        [Description("管理携程")] Coroutine m_COR;


        private bool IsHappened()
        {
            if (killCount == 0) return false;
            if (killCount % CodeLoad.Singleton.Config.RedEvent.TriggerCount != 0) return false;

            Cassie.Message("<color=red>[血宴狂欢]</color> 监测到血腥度超出预警值，SCP进入嗜血回复（10HP/s）+ 狂暴加速（207x1）状态", false, false, true);

            count = 0;

            m_COR = TimeCoroutine.Loop(
                CodeLoad.Singleton.Config.RedEvent.TotalCount,
                CodeLoad.Singleton.Config.RedEvent.LerpTime,
                () => this.DoNotice(),
                () => this.DoNoticeDone());

            return true;
        }

        private void DoNotice()
        {
         
            Cassie.Message($"<color=red>[血宴狂欢]</color> 压制程序准备中 ({(float)count / (float)CodeLoad.Singleton.Config.RedEvent.TotalCount:P0})", false, false, true);

            count += 1;

            PlayerUtils.TryGetPlayers(Team.SCPs, -1, out Player[] output);
            Array.ForEach(output, player => 
            {
                player.Heal(20f);
                player.ReferenceHub.playerEffectsController.ChangeState<Scp207>(1, CodeLoad.Singleton.Config.RedEvent.LerpTime, false);
            });
        }

        private void DoNoticeDone()
        {
            Cassie.Message($"<color=red>[血宴狂欢]</color> 血宴压制程序准备 100%，开始压制！", false, false, true);

            PlayerUtils.TryGetPlayers(Team.SCPs, -1, out Player[] output);
            Array.ForEach(output, player =>
            {
                player.Heal(15f);
                player.ReferenceHub.playerEffectsController.ChangeState<Scp207>(1, 1, false);
            });
        }
    }
}
